﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace GTA.Web
{
    public partial class GTAMaster : System.Web.UI.MasterPage
    {
        #region Properties
        public GTA.BusinessEntity.UserManagement.User LoggedUser
        {
            get { return Session["LoggedUser"] as GTA.BusinessEntity.UserManagement.User; }
            set { Session["LoggedUser"] = value; }
        }
        #endregion

        #region Starting Page
        protected void Page_Load(object sender, EventArgs e)
        {
            StartPage();
        }
        private void StartPage()
        {
            if (LoggedUser == null)
                Response.Redirect("~/Pages/Login.aspx");
            else
            {
                ucMessage.HideMessage();

                if (!IsPostBack)
                {
                    if (Global.Projects == null)
                        Global.Projects = new BusinessLogic.GTA().ListTeamProjects();

                    LoadUserData();
                    LoadInformativeSummary();
                }
            }
        }
        #endregion

        #region Events
        protected void lbLogoff_Click(Object sender, EventArgs e)
        {
            LogOffUser();
        }
        protected void ibUpdateInformativeSummary_Click(object sender, EventArgs e)
        {
            LoadInformativeSummary();
        }
        #endregion

        #region Methods
        public void HandleException(String operation, Exception ex)
        {
            ucMessage.ShowMessage(operation + "<br/>Detalhes:&nbsp;" + GTA.Common.Util.GetFullException(ex), GTA.Web.UserControls.ucMessage.MessageType.Error);
        }
        private void LoadUserData()
        {
            if (LoggedUser != null)
            {
                lblLoggedUserName.Text = LoggedUser.Name;
            }
        }
        private void LoadInformativeSummary()
        {
            //try
            //{
            //    LoadInformativeSummaryProjects();
            //    LoadInformativeSummaryArchitects();
            //    LoadInformativeSummaryStates();
            //    LoadInformativeSummaryCategories();
            //}
            //catch (Exception ex)
            //{
            //    HandleException("Carregar os resumos informativos.", ex);
            //}
        }
        //private void LoadInformativeSummaryProjects()
        //{
        //    try
        //    {
        //        GTA.BusinessLogic.GTA bl = GTA.BusinessLogic.GTA.GetInstance();
        //        List<GTA.BusinessEntity.InformativeSummaryProjects> summary = bl.ListInformativeSummaryProjects(LoggedUser, new GTA.BusinessEntity.TaskFilter());
        //        rptSummaryProjects.DataSource = summary;
        //        rptSummaryProjects.DataBind();
        //    }
        //    catch (Exception ex)
        //    {
        //        HandleException("Ocorreu um erro durante a obtenção do resumo informativo de projetos", ex);
        //    }
        //}
        //private void LoadInformativeSummaryArchitects()
        //{
        //    try
        //    {
        //        GTA.BusinessLogic.GTA bl = GTA.BusinessLogic.GTA.GetInstance();
        //        List<GTA.BusinessEntity.InformativeSummaryArchitects> summary = bl.ListInformativeSummaryArchitects(LoggedUser, new GTA.BusinessEntity.TaskFilter());
        //        rptSummaryArchitects.DataSource = summary;
        //        rptSummaryArchitects.DataBind();
        //    }
        //    catch (Exception ex)
        //    {
        //        HandleException("Ocorreu um erro durante a obtenção do resumo informativo de arquitetos.", ex);
        //    }
        //}
        //private void LoadInformativeSummaryStates()
        //{
        //    try
        //    {
        //        GTA.BusinessLogic.GTA bl = GTA.BusinessLogic.GTA.GetInstance();
        //        List<GTA.BusinessEntity.InformativeSummaryStates> summary = bl.ListInformativeSummaryStates(LoggedUser, new GTA.BusinessEntity.TaskFilter());
        //        rptSummaryStatus.DataSource = summary;
        //        rptSummaryStatus.DataBind();
        //    }
        //    catch (Exception ex)
        //    {
        //        HandleException("Ocorreu um erro durante a obtenção do resumo informativo de status.", ex);
        //    }
        //}
        //private void LoadInformativeSummaryCategories()
        //{
        //    try
        //    {
        //        GTA.BusinessLogic.GTA bl = GTA.BusinessLogic.GTA.GetInstance();
        //        List<GTA.BusinessEntity.InformativeSummaryCategories> summary = bl.ListInformativeSummaryCategories(LoggedUser, new GTA.BusinessEntity.TaskFilter());
        //        rptSummaryCategory.DataSource = summary;
        //        rptSummaryCategory.DataBind();
        //    }
        //    catch (Exception ex)
        //    {
        //        HandleException("Ocorreu um erro durante a obtenção do resumo informativo de categorias.", ex);
        //    }
        //}
        private void LogOffUser()
        {
            try
            {
                LoggedUser = null;
                Response.Redirect("~/Pages/Login.aspx");
            }
            catch (Exception ex)
            {
                HandleException("Deslogar o usuário.", ex);
            }
        }
        public void ShowMessage(string message, GTA.Web.UserControls.ucMessage.MessageType type)
        {
            ucMessage.ShowMessage(message, type);
        }
        #endregion
    }
}
